package com.xnx3.banmahuishou.agency.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.BaseVO;
import com.xnx3.banmahuishou.core.entity.GoodsPrice;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;
import com.xnx3.wangmarket.shop.core.entity.Goods;

/**
 * 代理商价格管理
 * @author qyw
 */
@Controller(value="BanmahuishouAgencyGoodsPriceController")
@RequestMapping("/agency/goodsPrice/")
public class GoodsPriceController extends BaseController{

	
	@Resource
	private SqlService sqlService;
	
	
	/**
	 * 代理商商品价格列表
	 */
	@RequestMapping("list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		
		// 先查出所有商品列表,在看看价格表是否有该代理商的 有的话给替换
		Sql sql = new Sql(request);
		//配置查询那个表
		sql.setSearchTable("shop_goods");
		//查询条件
		sql.appendWhere("isdelete = " + Goods.ISDELETE_NORMAL);
		//配置按某个字端搜索内容
		sql.setSearchColumn(new String[] {"title","typeid="});
		// 查询数据表的记录总条数
		int count = sqlService.count("shop_goods", sql.getWhere());
		
		// 配置每页显示15条
		Page page = new Page(count, 15, request);
		// 查询出总页数
		sql.setSelectFromAndPage("SELECT * FROM shop_goods ", page);
		//选择排序方式 当用户没有选择排序方式时，系统默认降序排序
		sql.setDefaultOrderBy("rank ASC");
		// 按照上方条件查询出该实体总数 用集合来装
		List<Goods> list = sqlService.findBySql(sql,Goods.class);
		// 查询代理商商品金额表,看看是否有数据
		String sqlString = "SELECT * FROM laji_goods_price WHERE agencyid = " + getUserId();
		List<GoodsPrice> goodsPrices = sqlService.findBySqlQuery(sqlString, GoodsPrice.class);
		if (goodsPrices != null && goodsPrices.size() > 0) {
			for (Goods goods : list) {
				for (GoodsPrice goodsPrice : goodsPrices) {
					if (goods.getId() - goodsPrice.getGoodsid() == 0) {
						// 该商品被代理商修改了价格,给附上价格
						goods.setMinMoney(goodsPrice.getMinMoney());
						goods.setMaxMoney(goodsPrice.getMaxMoney());
					}
				}
			}
		}
		
		model.addAttribute("page", page);
		model.addAttribute("list", list);
		ActionLogUtil.insert(request, "代理商查询商品价格", "第"+page.getCurrentPageNumber()+"页");
		return "/agency/goodsPrice/list";
	}
	
	
	

	/**
	 * 代理商修改商品价格
	 * @param id 		要修改的商品id
	 * @param minPrice	原商品最低价格
	 * @param maxPrice	原商品最高价格
	 */
	@RequestMapping("edit${url.suffix}")
	public String edit(
			@RequestParam(value = "id", required = false, defaultValue="0") Integer id,
			@RequestParam(value = "minPrice", required = false, defaultValue="0") Integer minPrice,
			@RequestParam(value = "maxPrice", required = false, defaultValue="0") Integer maxPrice,
			Model model, HttpServletRequest request){
		
		//编辑页面
		model.addAttribute("id", id);
		model.addAttribute("minPrice", minPrice);
		model.addAttribute("maxPrice", maxPrice);
		ActionLogUtil.insert(request, "代理商进入商品价格修改页面");
		return "/agency/goodsPrice/edit";
	}
	
	/**
	 * 代理商修改商品价格的进行保存
	 * @param goodsid 		要修改的商品id
	 * @param minPrice	现商品最低价格
	 * @param maxPrice	现商品最高价格
	 */
	@RequestMapping(value="save.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO save(
			@RequestParam(value = "goodsid", required = false, defaultValue="0") Integer goodsid,
			@RequestParam(value = "minPrice", required = false, defaultValue="0") Double minPrice,
			@RequestParam(value = "maxPrice", required = false, defaultValue="0") Double maxPrice,
			HttpServletRequest request){
		
		if (goodsid <= 0) {
			return error("请传入修改的商ID");
		}
		if (minPrice <= 0) {
			return error("请传入商品最低价格");
		}
		if (maxPrice <= 0) {
			return error("请传入商品最高价格");
		}
		// 保存商品价格
		String string = "SELECT * FROM laji_goods_price WHERE agencyid = " + getUserId() + " AND goodsid = " + goodsid;
		GoodsPrice goodsPrice = sqlService.findAloneBySqlQuery(string, GoodsPrice.class);
		if (goodsPrice == null) {
			goodsPrice = new GoodsPrice();
			goodsPrice.setAgencyid(getUserId());
			goodsPrice.setGoodsid(goodsid);
		}
		goodsPrice.setMaxMoney((int)(maxPrice * 100));
		goodsPrice.setMinMoney((int)(minPrice * 100));
		sqlService.save(goodsPrice);
		ActionLogUtil.insertUpdateDatabase(request, getUserId(),"代理商修改商品价格", "商品价格信息: " + goodsPrice.toString());
		return success();
	}
	
}
